#
# FreeModbus MCF5235 port- Makefile
#
# Copyright (c) 2006 Christian Walter, Vienna 2006.
#
# $Id: Makefile,v 1.1 2006/08/30 23:18:06 wolti Exp $
#
# ---------------------------------------------------------------------------

BASE        = /opt/gcc-m68k/bin
CC	        = $(BASE)/m68k-elf-gcc
CXX	        = $(BASE)/m68k-elf-g++
OBJCOPY	    = $(BASE)/m68k-elf-objcopy
SIZE	    = $(BASE)/m68k-elf-size
INSIGHT     = $(BASE)/m68k-bdm-elf-insight
BDMFLASH    = $(BASE)/bdmflash

#CFLAGS	    = -MD -O2 -m528x -Wall 
MB_INC      = -Iport -I../../modbus/tcp -I../../modbus/include
RTOS_INC    = -Ifreertos/include -I../Common/include
LWIP_INC    = -Ilwip/src/include -Ilwip/src/include/ipv4 \
              -Ilwip/src/include -Ilwip/src/include/ipv4 \
              -Ilwip/contrib/port/FreeRTOS/MCF5235 \
              -Ilwip/contrib/port/FreeRTOS/MCF5235/netif
CFLAGS	    = -MD -gdwarf-2 -g3 -m528x -Wall \
			  -D'GCC_MCF5235=1' -D'_GCC_USES_FP=1' \
			  -D'__IPSBAR=((vuint8 *) 0x40000000)' -D'FSYS_2=25000000UL'
CFLAGS      += -I. -Iinclude -Iinclude/arch $(RTOS_INC) $(MB_INC) $(LWIP_INC)

ASFLAGS     = -MD -gdwarf-2 -g3 -m528x -Wa,--register-prefix-optional \
              -Wa,--bitwise-or -Wa,--defsym,IPSBAR=0x40000000
LDSCRIPT    = m5235-ram.ld
LDFLAGS	    = -nostartfiles -m528x -Wl,--script=$(LDSCRIPT)

TGT         = demo
OTHER_CSRC  = 
OTHER_ASRC  = $(addprefix system/, crt0.S vector.S)
MB_SRC      = $(addprefix ../../modbus/, mb.c tcp/mbtcp.c functions/mbfunccoils.c functions/mbfuncdiag.c functions/mbfuncholding.c functions/mbfuncinput.c functions/mbfuncother.c functions/mbfuncdisc.c functions/mbutils.c) \
              $(addprefix port/, porttcp.c portevent.c portother.c )

CSRC        = demo.c $(MB_SRC) \
              $(addprefix system/, init.c newlib.c serial.c) \
			  $(addprefix freertos/, tasks.c queue.c list.c) \
			  $(addprefix freertos/portable/MemMang/, heap_3.c) \
			  $(addprefix freertos/portable/GCC/MCF5235/, port.c) \
              $(addprefix lwip/src/core/, tcp_out.c inet.c mem.c memp.c netif.c pbuf.c raw.c stats.c sys.c tcp.c tcp_in.c udp.c ipv4/ip.c ipv4/ip_addr.c ipv4/icmp.c ipv4/ip_frag.c) \
              $(addprefix lwip/src/api/, tcpip.c api_msg.c err.c api_lib.c ) \
              $(addprefix lwip/src/netif/, etharp.c ) \
              $(addprefix lwip/contrib/port/FreeRTOS/MCF5235/, sys_arch.c netif/fec.c netif/nbuf.c) 

ASRC        = $(addprefix system/, mcf5xxx.S ) 
OBJS        = $(CSRC:.c=.o) $(ASRC:.S=.o)
NOLINK_OBJS = $(OTHER_CSRC:.c=.o) $(OTHER_ASRC:.S=.o)
DEPS        = $(OBJS:.o=.d) $(NOLINK_OBJS:.o=.d)
BIN         = $(TGT).elf

.PHONY: clean all

all: $(BIN)

flash-programm: $(TGT).elf
	$(OBJCOPY) -O binary $(TGT).elf $(TGT).bin
	@BIN_SIZE=`du -b $(TGT).bin | awk '//{ print $$1; }'`; \
	echo "programming $(TGT).bin with size $$BIN_SIZE to flash..."; \
	$(BDMFLASH) /dev/bdmcf20 0x00000000 1 2 write $(TGT).bin 0

flash-verify:
	@BIN_SIZE=`du -b $(TGT).bin | awk '//{ print $$1; }'`; \
	echo "loading $$BIN_SIZE bytes from target into $(TGT).vrf..."; \
	$(BDMFLASH) /dev/bdmcf20 0x00000000 1 2 read $(TGT).vrf 0 $$BIN_SIZE

flash-erase:
	$(BDMFLASH) /dev/bdmcf20 0x00000000 1 2 erase

debug:
	$(INSIGHT) --command=m5235.gdb --se=$(TGT).elf

$(BIN): $(OBJS) $(NOLINK_OBJS)
	$(CC) $(LDFLAGS) -Wl,-Map=$(TGT).map $(OBJS) $(LDLIBS) -o $@

clean:
	rm -f $(DEPS)
	rm -f $(OBJS) $(NOLINK_OBJS)
	rm -f $(BIN) $(TGT).map

# ---------------------------------------------------------------------------
# rules for code generation
# ---------------------------------------------------------------------------
%.o:    %.c
	$(CC) $(CFLAGS) -o $@ -c $<

%.o:    %.S
	$(CC) $(ASFLAGS) -o $@ -c $<

# ---------------------------------------------------------------------------
#  # compiler generated dependencies
# ---------------------------------------------------------------------------
-include $(DEPS)

